package com.lnking.lnkingplace.service.impl;

import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.PlaceInfoMapper;
import com.lnking.lnkingplace.mapper.SuggestMapper;
import com.lnking.lnkingplace.model.entity.Suggest;
import com.lnking.lnkingplace.model.enums.SuggestType;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.suggest.SuggestAddRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestUpdateRequest;
import com.lnking.lnkingplace.service.ISuggestService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-19
 */
@Service
public class SuggestServiceImpl extends CustomServiceImpl<SuggestMapper, Suggest> implements ISuggestService {

    @Resource
    UserUtil userUtil;

    @Resource
    PlaceInfoMapper placeInfoMapper;
    @Override
    public void add(SuggestAddRequest params) throws Exception {
        Suggest entity = new Suggest();
        BeanUtils.copyProperties(params, entity);
        entity.setCreateTime(LocalDateTime.now());
        //获取用户信息并写入
        String channel = userUtil.getUser().getChannel();
        String userId = userUtil.getUser().getId();
        String userName = userUtil.getUser().getName();
        entity.setCreatorName(userName);
        entity.setChannel(channel);

        //个人建议
        if(userUtil.getUser().getType().equals(UserType.USER)){
            entity.setUserId(userId);
            entity.setType(SuggestType.USER);
        }
        //场所建议
        else if(userUtil.getUser().getType().equals(UserType.LOCATION)){
            entity.setPlaceId(userUtil.getUser().getPlaceId());
            entity.setType(SuggestType.PLACE);
        }

        //写入填写人电话和所在场所
        entity.setWriterPhone(userUtil.getUser().getPhone());
        String placeName = placeInfoMapper.getPlaceName(userUtil.getUser().getPlaceId());
        entity.setPlaceName(placeName);

        getBaseMapper().insert(entity);
    }

    @Override
    public void update(SuggestUpdateRequest params) {
        Suggest entity = new Suggest();
        BeanUtils.copyProperties(params, entity);
        getBaseMapper().updateById(entity);
    }

    @Override
    public void updateAnswer(String id, String answer) {
        this.baseMapper.updateAnswer(id,answer);
    }
}
